package com.rxyb.upms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rxyb.core.respone.R;
import com.rxyb.upms.db.bean.SysMenuResouce;
import com.rxyb.upms.db.bean.SysRole;
import com.rxyb.upms.db.bean.SysRoleMenu;
import com.rxyb.upms.db.bean.SysUser;
import com.rxyb.upms.db.dao.SysRoleMapper;
import com.rxyb.upms.gate.pojo.UserDetailReq;
import com.rxyb.upms.gate.pojo.UserDetailRsp;
import com.rxyb.upms.service.SysRoleService;
import com.rxyb.upms.service.logic.SysMenuResourceLogic;
import com.rxyb.upms.service.logic.SysRoleLogic;
import com.rxyb.upms.service.logic.SysRoleMenuLogic;
import com.rxyb.upms.service.logic.SysUserLogic;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author YJH
 * @since 2020-05-26
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private final SysRoleLogic sysRoleLogic;

    private final SysRoleMenuLogic sysRoleMenuLogic;

    private final SysUserLogic sysUserLogic;

    private final SysMenuResourceLogic sysMenuResourceLogic;


    public SysRoleServiceImpl(SysRoleLogic sysRoleLogic, SysRoleMenuLogic sysRoleMenuLogic, SysUserLogic sysUserLogic, SysMenuResourceLogic sysMenuResourceLogic) {
        this.sysRoleLogic = sysRoleLogic;
        this.sysRoleMenuLogic = sysRoleMenuLogic;
        this.sysUserLogic = sysUserLogic;
        this.sysMenuResourceLogic = sysMenuResourceLogic;
    }

    public R<UserDetailRsp> findUserDetail(UserDetailReq req) {

        UserDetailRsp rsp = new UserDetailRsp();
        SysUser userName = sysUserLogic.findUserName(req.getUserName());
        if (null == userName) {
            return R.failed("未获取到用户信息");
        }

        rsp.setSysUser(userName);
        // 角色信息
        Optional<SysRole> userNameRole = sysRoleLogic.findUserNameRole(Long.valueOf(userName.getRoleId()));
        if (!userNameRole.isPresent()) {
            return R.failed("未获取到权限信息");
        }

        rsp.setRoles(new String[]{userNameRole.get().getId().toString()});
        // 角色菜单关连信息
        Optional<List<SysRoleMenu>> roleId = sysRoleMenuLogic.findRoleId(userNameRole.get().getId());
        if (!roleId.isPresent()) {
            return R.failed("未获取到角色信息");
        }
        List<Long> menuIds = roleId.get().stream().filter(
                role -> StringUtils.isNotEmpty(role.getMenuId().toString())).map(SysRoleMenu::getMenuId).collect(Collectors.toList());

        Optional<List<SysMenuResouce>> findmenus = sysMenuResourceLogic.findmenus(menuIds.toArray(new Long[menuIds.size()]));
        if (!findmenus.isPresent()) {
            return R.failed("未获取到菜单信息");
        }
        List<String> collect = findmenus.get().stream().map(SysMenuResouce::getMenuName).collect(Collectors.toList());
        rsp.setSysMenuResouces(collect.toArray(new String[collect.size()]));
        return R.ok(rsp);
    }
}
